package cn.ibizlab.odoo.core.odoo_project.service.impl;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.math.BigInteger;
import java.util.Collection;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import cn.ibizlab.odoo.core.client.model.Iproject_tags;
import cn.ibizlab.odoo.core.odoo_project.domain.Project_tags;
import cn.ibizlab.odoo.core.odoo_project.filter.Project_tagsSearchContext;
import cn.ibizlab.odoo.core.odoo_project.service.IProject_tagsService;
import cn.ibizlab.odoo.util.SearchResult;
import cn.ibizlab.odoo.util.log.IBIZLog;

/**
 * 实体[项目标签] 服务对象接口实现
 */
@Slf4j
@IBIZLog
@Service
public class Project_tagsServiceImpl implements IProject_tagsService {

    @Autowired
    private cn.ibizlab.odoo.core.client.service.Iproject_tagsClientService project_tagsClientService;

    @Override
    public Project_tags get(Integer id) {
        Iproject_tags clientModel = project_tagsClientService.createModel();
        clientModel.setId(id);
        project_tagsClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public Project_tags get(Project_tags project_tags) {
        Project_tags retproject_tags = this.get(project_tags.getId()) ;
    	BeanCopier copier=BeanCopier.create(retproject_tags.getClass(), project_tags.getClass(), false);
        copier.copy(retproject_tags, project_tags, null);
        return project_tags ;
    }

    @Override
    public void createBatch(List<Project_tags> project_tagss, int batchSize) {

                        //project_tagsClientService.createBatch(project_tagss,batchSize) ;

    }

    @Override
    public boolean remove(Integer id) {
        boolean bOk = false ;
        Iproject_tags clientModel = project_tagsClientService.createModel();
        clientModel.setId(id);
        project_tagsClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

    @Override
    public boolean create(Project_tags project_tags) {
        boolean bOk = false ;
        Iproject_tags clientModel = this.convert2Model(project_tags,null);
        project_tagsClientService.create(clientModel) ;
        this.convert2Domain(clientModel,project_tags);
        bOk = true ;

        return bOk ;
    }

    @Override
    public void updateBatch(List<Project_tags> project_tagss, int batchSize) {

        //project_tagsClientService.updateBatch(project_tagss,batchSize) ;

    }

    @Override
    public boolean update(Project_tags project_tags) {
        boolean bOk = false ;
        Iproject_tags clientModel = this.convert2Model(project_tags,null);
        project_tagsClientService.update(clientModel) ;
        this.convert2Domain(clientModel,project_tags) ;
        bOk = true;
        return bOk ;
    }

    @Override
    public void removeBatch(Collection<? extends Serializable> idList, int batchSize) {

        //project_tagsClientService.removeBatch(project_tagss) ;

    }

    /**
     * 查询集合 默认查询
     */
    @Override
    public Page<Project_tags> searchDefault(Project_tagsSearchContext context) {
        List<Project_tags> list = new ArrayList<Project_tags>();
        Page<cn.ibizlab.odoo.core.client.model.Iproject_tags> clientModelList = project_tagsClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Iproject_tags clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Project_tags>(list, context.getPageable(), clientModelList.getTotalElements());
    }


    /**
     * 根据用户查询
     */
    @Override
    public Page<Project_tags> selectByOdoo_create(Integer id){
        Project_tagsSearchContext context = new Project_tagsSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Project_tags> list = new ArrayList<Project_tags>();
        Page<cn.ibizlab.odoo.core.client.model.Iproject_tags> clientModelList = project_tagsClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Iproject_tags clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Project_tags>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Project_tags> selectByOdoo_write(Integer id){
        Project_tagsSearchContext context = new Project_tagsSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Project_tags> list = new ArrayList<Project_tags>();
        Page<cn.ibizlab.odoo.core.client.model.Iproject_tags> clientModelList = project_tagsClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Iproject_tags clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Project_tags>(list, context.getPageable(), clientModelList.getTotalElements());

    }



    public Iproject_tags convert2Model(Project_tags domain , Iproject_tags model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = project_tagsClientService.createModel() ;
        }

         if(domain.getNameDirtyFlag())
            model.setName(domain.getName());
         if(domain.getWrite_dateDirtyFlag())
            model.setWrite_date(domain.getWrite_date());
         if(domain.getDisplay_nameDirtyFlag())
            model.setDisplay_name(domain.getDisplay_name());
         if(domain.getColorDirtyFlag())
            model.setColor(domain.getColor());
         if(domain.getIdDirtyFlag())
            model.setId(domain.getId());
         if(domain.get__last_updateDirtyFlag())
            model.set__last_update(domain.get__last_update());
         if(domain.getCreate_dateDirtyFlag())
            model.setCreate_date(domain.getCreate_date());
         if(domain.getWrite_uid_textDirtyFlag())
            model.setWrite_uid_text(domain.getWrite_uid_text());
         if(domain.getCreate_uid_textDirtyFlag())
            model.setCreate_uid_text(domain.getCreate_uid_text());
         if(domain.getCreate_uidDirtyFlag())
            model.setCreate_uid(domain.getCreate_uid());
         if(domain.getWrite_uidDirtyFlag())
            model.setWrite_uid(domain.getWrite_uid());
        return model ;
    }

    public Project_tags convert2Domain( Iproject_tags model ,Project_tags domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new Project_tags();
        }

        if(model.getNameDirtyFlag())
            domain.setName(model.getName());
        if(model.getWrite_dateDirtyFlag())
            domain.setWrite_date(model.getWrite_date());
        if(model.getDisplay_nameDirtyFlag())
            domain.setDisplay_name(model.getDisplay_name());
        if(model.getColorDirtyFlag())
            domain.setColor(model.getColor());
        if(model.getIdDirtyFlag())
            domain.setId(model.getId());
        if(model.get__last_updateDirtyFlag())
            domain.set__last_update(model.get__last_update());
        if(model.getCreate_dateDirtyFlag())
            domain.setCreate_date(model.getCreate_date());
        if(model.getWrite_uid_textDirtyFlag())
            domain.setWrite_uid_text(model.getWrite_uid_text());
        if(model.getCreate_uid_textDirtyFlag())
            domain.setCreate_uid_text(model.getCreate_uid_text());
        if(model.getCreate_uidDirtyFlag())
            domain.setCreate_uid(model.getCreate_uid());
        if(model.getWrite_uidDirtyFlag())
            domain.setWrite_uid(model.getWrite_uid());
        return domain ;
    }


}
